<?php

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport('joomla.plugin.plugin');

class plgUserProf3taacl extends JPlugin
{
    function plgUserProf3taacl(& $subject) {
        parent::__construct($subject);
    }

    function onPerformingTask($aroId = NULL, $axoId = NULL, $acoName = NULL) {
        return prof3taaclManager::authorize($aroId, $axoId, $acoName);
    }

}

class prof3taaclManager {

        function authorize($aroId = NULL, $axoId = NULL, $acoName = NULL){
                
		$groupId = prof3taaclManager::getGroupId($aroId);
		$acoId = prof3taaclManager::getAcoId($acoName, $axoId);

                $db =& JFactory::getDBO();

                $query =  ' SELECT COUNT(*)'
                        . ' FROM #__acl_aro_groups_acos_map AS map'
                        . ' WHERE map.aro_group_id = ' . $groupId
                        . ' AND map.aco_id = ' . $acoId . ';';

                $db->setQuery($query, 0);
                $result = $db->loadResult();

                return ($result == '1') ? true : false;

        }

        function getGroupId($userId){

                $db =& JFactory::getDBO();

                $query =  ' SELECT aro_group_id'
                        . ' FROM #__acl_aros_aro_groups_map AS map'
                        . ' WHERE map.aro_id = ' . $userId
                        . ' LIMIT 1' . ';';

                $db->setQuery($query, 0);
                $result = $db->loadResult();

                return $result;

        }

        function getAcoId($acoName, $axoId){

                $db =& JFactory::getDBO();

                $query =  ' SELECT id'
                        . ' FROM #__acl_acos'
                        . ' WHERE aco_name = \'' . $acoName . '\''
			. ' AND axo_id = ' . $axoId
                        . ' LIMIT 1' . ';';
                
		$db->setQuery($query, 0);
                $result = $db->loadResult();

                return $result;

        }

}

?>
